﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using iTeachSoft.CoreObjects;
using iTeachSoft.Code.Utility;
using System.Drawing;
using iTeachSoft.Presentation.Code.Utility;
using iTeachSoft.Code.Managers;
using iTeachSoft.Presentation.Code.Managers;
using iTeachSoft.Presentation.Code.ExtensionClasses;

namespace iTeachSoft.Presentation.Dialogs
{
    /// <summary>
    /// Interaction logic for DialogInsertFrame.xaml
    /// </summary>
    public partial class DialogInsertFrame : Window
    {
        private int mOriginalWidth = -1;
        private int mOriginalHeight = -1;

        public DialogInsertFrame()
        {
            InitializeComponent();

            textBoxImageHeight.Text = RuntimeStorage.CurrentProject.SlidesSize.Height.ToString();
            textBoxImageWidth.Text = RuntimeStorage.CurrentProject.SlidesSize.Width.ToString();

            textBoxImageWidth.LostFocus += new RoutedEventHandler(textBoxImageWidth_LostFocus);
            textBoxImageHeight.LostFocus += new RoutedEventHandler(textBoxImageHeight_LostFocus);
        }

        void textBoxImageHeight_LostFocus(object sender, RoutedEventArgs e)
        {
            int value = ParseUtility.CheckValue(textBoxImageHeight);
            if (!checkBoxPreserveRatio.IsChecked.Value) return;

            int newValue = 0;
            if (value >= 0)
                newValue = (int)((float)value * ((float)mOriginalWidth / (float)mOriginalHeight));

            textBoxImageWidth.Text = newValue.ToString();
        }

        void textBoxImageWidth_LostFocus(object sender, RoutedEventArgs e)
        {
            int value = ParseUtility.CheckValue(textBoxImageWidth);
            if (!checkBoxPreserveRatio.IsChecked.Value) return;

            int newValue = 0;
            if (value >= 0)
                newValue = (int)((float)value * ((float)mOriginalHeight / (float)mOriginalWidth));

            textBoxImageHeight.Text = newValue.ToString();
        }

        private void buttonBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Image files | *.png;*.jpg;*.jpeg;*.bmp";

            bool result = dlg.ShowDialog().Value;
            if (result == true)
            {
                textBoxImagePath.Text = dlg.FileName;

                Bitmap image = new Bitmap(dlg.FileName);

                mOriginalWidth = image.Width;
                mOriginalHeight = image.Height;
            }
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            if (!System.IO.File.Exists(textBoxImagePath.Text))
            {
                AlertUtility.ShowError("Please select an image first!");
                return;
            }

            System.Drawing.Image frameImage = new Bitmap(textBoxImagePath.Text);
            Slide newSlide = new Slide();

            newSlide.SaveSlideImageToDisk(frameImage);
            newSlide.PreserveRation = checkBoxPreserveRatio.IsChecked.Value;

            //the values in the text boxes should be correct
            newSlide.Size = new System.Drawing.Size(int.Parse(textBoxImageWidth.Text), int.Parse(textBoxImageHeight.Text));

            RuntimeStorage.CurrentProject.Slides.Add(newSlide);
            EventsManagement.Instance.OnReflectProjectInvoked(this, EventArgs.Empty);
            //Select the newly added slide
            EventsManagement.Instance.OnSelectedSlideIndexChanged(this, new SingleItemEventArgs<int>(RuntimeStorage.CurrentProject.Slides.Count - 1));

            this.Close();
        }
    }
}
